import json
import os.path

from utils.csvtool import CsvReader
from utils.exceltool import ExcelReader
from utils.yamltool import YamlReader, sysConf, BasePath


def get_yaml_cases(filename) -> list:
	filename = os.path.join(BasePath, 'data', filename)
	data = YamlReader(filename).data
	base = data.get('base')
	cases = data.get('testCase')
	case_list = []
	for _case in cases:
		_case['url'] = base.get('url') if base.get('url').startswith('http') else sysConf.get('test').get('domain') + base.get('url')
		_case['headers'] = base.get('headers')
		_case['method'] = base.get('method')
		case_list.append(_case)
	return case_list


def get_xls_cases(filename) -> list:
	data = []
	for _case in ExcelReader(filename).data:
		_case['url'] = _case.get('url') if _case.get('url').startswith('http') else sysConf.get('test').get('domain') + _case.get('url')
		_case['headers'] = json.loads(_case.get('headers').replace('\'', '"').replace('`', '\''))
		_case['expected'] = json.loads(_case.get('expected').replace('\'', '"'))
		_case['data'] = json.loads(_case.get('data').replace('\'', '"'))
		data.append(_case)
	return data


def get_csv_cases(filename) -> list:
	data = []
	for _case in CsvReader(filename).data:
		_case['url'] = _case.get('url') if _case.get('url').startswith('http') else sysConf.get('test').get('domain') + _case.get('url')
		_case['headers'] = json.loads(_case.get('headers').replace('\'', '"').replace('`', '\''))
		_case['expected'] = json.loads(_case.get('expected').replace('\'', '"'))
		_case['data'] = json.loads(_case.get('data').replace('\'', '"'))
		data.append(_case)
	return data
